/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tela;

import classPack.Conversa;
import classPack.Mensagem;
import classPack.Usuario;
import javax.swing.JTabbedPane;

/**
 *
 * @author ja21391
 */
public class PanelConversa extends javax.swing.JPanel {

    private JTabbedPane pai;
    private Conversa chat;
    private String texto;
    private final String inicio = "<html><head><title>Teste</title></head><body>",
            fim = "</body></html>";

    /**
     * Creates new form telaConversa
     */
    public PanelConversa(JTabbedPane pai, Conversa chat) {
        this.chat = chat;
        initComponents();
        this.pai = pai;
        montarUsuarios();
        mostrar();
        texto = "";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTxtARecebidos = new javax.swing.JEditorPane();
        jPanel4 = new javax.swing.JPanel();
        jLabUsus = new javax.swing.JLabel();
        jBtnFechar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTxtAEnviar = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        jBtnEnviar = new javax.swing.JButton();
        jCbEnter = new javax.swing.JCheckBox();
        jBtnHist = new javax.swing.JButton();

        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        jSplitPane2.setDividerLocation(240);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jPanel1.setLayout(new java.awt.BorderLayout());

        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        jTxtARecebidos.setEditable(false);
        jTxtARecebidos.setContentType("text/html"); // NOI18N
        jTxtARecebidos.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        jScrollPane2.setViewportView(jTxtARecebidos);

        jPanel1.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel4.setLayout(new java.awt.BorderLayout());

        jLabUsus.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabUsus.setText("jLabel1");
        jPanel4.add(jLabUsus, java.awt.BorderLayout.LINE_START);

        jBtnFechar.setFont(new java.awt.Font("Courier New", 1, 12)); // NOI18N
        jBtnFechar.setForeground(new java.awt.Color(102, 0, 0));
        jBtnFechar.setText("X");
        jBtnFechar.setPreferredSize(new java.awt.Dimension(40, 23));
        jBtnFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnFecharActionPerformed(evt);
            }
        });
        jPanel4.add(jBtnFechar, java.awt.BorderLayout.LINE_END);

        jPanel1.add(jPanel4, java.awt.BorderLayout.PAGE_START);

        jSplitPane2.setTopComponent(jPanel1);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane4.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane4.setHorizontalScrollBar(null);

        jTxtAEnviar.setColumns(20);
        jTxtAEnviar.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        jTxtAEnviar.setRows(5);
        jTxtAEnviar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTxtAEnviarKeyPressed(evt);
            }
        });
        jScrollPane4.setViewportView(jTxtAEnviar);

        jPanel2.add(jScrollPane4, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.GridLayout(3, 1));

        jBtnEnviar.setText("Enviar");
        jBtnEnviar.setEnabled(false);
        jBtnEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEnviarActionPerformed(evt);
            }
        });
        jPanel3.add(jBtnEnviar);

        jCbEnter.setSelected(true);
        jCbEnter.setText("Enter Envina");
        jCbEnter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCbEnterActionPerformed(evt);
            }
        });
        jPanel3.add(jCbEnter);

        jBtnHist.setText("Historico");
        jBtnHist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnHistActionPerformed(evt);
            }
        });
        jPanel3.add(jBtnHist);

        jPanel2.add(jPanel3, java.awt.BorderLayout.LINE_END);

        jSplitPane2.setRightComponent(jPanel2);

        add(jSplitPane2);
    }// </editor-fold>//GEN-END:initComponents

    public void addMensagem(String mensagens) {
        texto += mensagens;
        jTxtARecebidos.setText(inicio + texto + fim);
        jTxtARecebidos.setCaretPosition(jTxtARecebidos.getDocument().getLength());
    }

    public void mostrar() {
        this.pai.add(chat.getTitulo(), this);
        this.setVisible(true);
    }

    public void montarUsuarios() {
        String label = "";
        for (Usuario user : chat.getUsuarios().values()) {
            label += user.getNome() + "; ";
        }
        jLabUsus.setText(label);
    }

    public void fecharJanela() {
        pai.remove(this);
        this.setVisible(false);
    }

    private void jBtnEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEnviarActionPerformed
        // TODO add your handling code here:
        if (!jTxtAEnviar.getText().equals("")) {
            Mensagem nova = new Mensagem(Principal.logado, jTxtAEnviar.getText());
            //System.out.println(nova);
            chat.enviaMensagem(nova);
            jTxtAEnviar.setText("");
        }
    }//GEN-LAST:event_jBtnEnviarActionPerformed

    private void jBtnFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnFecharActionPerformed
        // TODO add your handling code here:
        fecharJanela();
    }//GEN-LAST:event_jBtnFecharActionPerformed

    private void jTxtAEnviarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtAEnviarKeyPressed
        // TODO add your handling code here:
        //System.out.println(evt.getKeyChar());
        if (jCbEnter.isSelected() && evt.getKeyCode() == 10) {
            if (!evt.isShiftDown()) {
                jBtnEnviarActionPerformed(null);
                evt.consume();
            } else {
                jTxtAEnviar.setText(jTxtAEnviar.getText().concat("\n"));
            }
        }
    }//GEN-LAST:event_jTxtAEnviarKeyPressed

    private void jCbEnterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCbEnterActionPerformed
        // TODO add your handling code here:
        jBtnEnviar.setEnabled(!jCbEnter.isSelected());
    }//GEN-LAST:event_jCbEnterActionPerformed

    private void jBtnHistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnHistActionPerformed
        // TODO add your handling code here:
        chat.apagarHistorico();
        texto = "";
        addMensagem(texto);
    }//GEN-LAST:event_jBtnHistActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnEnviar;
    private javax.swing.JButton jBtnFechar;
    private javax.swing.JButton jBtnHist;
    private javax.swing.JCheckBox jCbEnter;
    private javax.swing.JLabel jLabUsus;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JTextArea jTxtAEnviar;
    private javax.swing.JEditorPane jTxtARecebidos;
    // End of variables declaration//GEN-END:variables

}
